package com.ihy.handler;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.session.InvalidSessionStrategy;
import org.springframework.stereotype.Component;
import com.ihy.vo.ResultResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@Component
public class InvalidSessionHandler implements InvalidSessionStrategy {
    @Autowired
    private ResultResponse resultResponse;

    @Override
    public void onInvalidSessionDetected(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/json;charset=utf8");
        resultResponse.setCode(407);
        resultResponse.setData(null);
        resultResponse.setMessage("登录超时");
        log.info("用户登录超时，访问[{}]失败", request.getRequestURI());
        response.getWriter().write(JSON.toJSONString(resultResponse));
    }
}